import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 定义一个静态路由
// 组织架构路由
import departments from '@/router/modules/departments'
// 员工路由
import employees from '@/router/modules/employees'
// 审批路由
import approvals from '@/router/modules/approvals'
// 考勤路由
import attendances from '@/router/modules/attendances'
// 权限管理路由
import permission from '@/router/modules/permission'
// 工资路由
import salarys from '@/router/modules/salarys'
// 公司设置路由
import setting from '@/router/modules/setting'
// 公司设置路由
import social from '@/router/modules/social'

export const constantRoutes = [{
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: {
        title: '首页',
        icon: 'dashboard'
      }
    }]
  },

  // 404 page must be placed at the end !!!
  {
    path: '*',
    redirect: '/404',
    hidden: true
  }
]

// 给一个静态路由
const syncRouter = [
  departments,
  employees,
  approvals,
  attendances,
  permission,
  salarys,
  setting,
  social
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({
    y: 0
  }),
  // routes: constantRoutes
  routes: [...constantRoutes, ...syncRouter]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
